import sys
sys.path.append("../")
import pymysql
from frameworks.cores.Config import *

class DB:
    db = ''
    cursor = ''

    def __init__(self):
        conf = Config().getDB()
        self.db = pymysql.connect(host=conf[0],user=conf[1],password=conf[2],port=conf[3],database=conf[4],charset='utf8')
        self.cursor = self.db.cursor()

    def executeSql(self,sql=''):
        try:
            # 执行SQL语句
            self.cursor.execute(sql)
            # 获取所有记录列表
            results = self.cursor.fetchall()
            return results

        except:
            print("Error: unable to fecth data")

    def add(self,sql):
        try:
            self.cursor.execute(sql)
            # 提交到数据库执行
            self.db.commit()
        except:
            self.db.rollback()
            print("Error: add error")

    def update(self,sql):
        try:
            self.cursor.execute(sql)
            # 提交到数据库执行
            self.db.commit()
        except:
            self.db.rollback()
            print("Error: update error")

    def delete(self,sql):
        try:
            self.cursor.execute(sql)
            # 提交到数据库执行
            self.db.commit()
        except:
            self.db.rollback()
            print("Error: delete error")


